package com.financial.service.heemoney.service.impl;

import com.financial.service.heemoney.constants.FinancialRegisterHeemoneylogEnumConstants;
import com.financial.service.heemoney.convertor.HeemoneyProviderSubmitConvert;
import com.financial.service.heemoney.convertor.HeemoneyRegisterConvert;
import com.financial.service.heemoney.dao.FinancialProsubmitHeemoneylogDao;
import com.financial.service.heemoney.dao.FinancialSubmitHeemoneylogDao;
import com.financial.service.heemoney.domain.FinancialProsubmitHeemoneylogDO;
import com.financial.service.heemoney.domain.FinancialSubmitHeemoneylogDO;
import com.financial.service.heemoney.dto.HeemoneyConfigDTO;
import com.financial.service.heemoney.dto.businsess.HeemoneyProviderSubmitBusinessDTO;
import com.financial.service.heemoney.dto.businsess.HeemoneyRegisterBusinessDTO;
import com.financial.service.heemoney.dto.commmon.HeemoneyProviderSubmitCommonDTO;
import com.financial.service.heemoney.dto.commmon.HeemoneyRegisterCommonDTO;
import com.financial.service.heemoney.dto.inparameter.HeemoneyProviderSubmitInParamDTO;
import com.financial.service.heemoney.dto.inparameter.HeemoneyRegisterInParamDTO;
import com.financial.service.heemoney.dto.param.HeemoneyProviderSubmitParamDTO;
import com.financial.service.heemoney.dto.param.HeemoneyRegisterParamDTO;
import com.financial.service.heemoney.service.FinancialProsubmitHeemoneylogService;
import com.financial.service.heemoney.utils.JsonUtils;
import com.financial.service.heemoney.utils.OkHttpUtils;
import com.financial.service.heemoney.utils.ResultUtils;
import com.financial.service.heemoney.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class FinancialProsubmitHeemoneylogServiceImpl implements FinancialProsubmitHeemoneylogService {
    @Autowired
    private FinancialProsubmitHeemoneylogDao financialProsubmitHeemoneylogDao;
    @Autowired
    private OkHttpUtils okHttpUtils;
    @Autowired
    private FinancialSubmitHeemoneylogDao financialSubmitHeemoneylogDao;
    public static final Logger log = LoggerFactory.getLogger(FinancialPayHeemoneyServiceImpl.class);

    @Override
    public ApiResult providerSubmit(HeemoneyProviderSubmitInParamDTO heemoneyProviderSubmitInParamDTO) {
        log.info("调用金融中心汇收银服务层商户统一进件接口前参数打印：{}", JsonUtils.beanToJson(heemoneyProviderSubmitInParamDTO));
        //请求参数日志保存
        FinancialSubmitHeemoneylogDO submitHeemoneylogDO = this.saveRequest(heemoneyProviderSubmitInParamDTO);
        //数据转实体校验参数非空
        HeemoneyProviderSubmitCommonDTO providerSubmitCommonDTO = HeemoneyProviderSubmitConvert.MAPPER.configDTOConvertCommonDTO(new HeemoneyConfigDTO(), heemoneyProviderSubmitInParamDTO);
        HeemoneyProviderSubmitBusinessDTO providerSubmitBusinessDTO = HeemoneyProviderSubmitConvert.MAPPER.configDTOConvertBusinessDTO(new HeemoneyConfigDTO(), heemoneyProviderSubmitInParamDTO);
        HeemoneyProviderSubmitParamDTO heemoneyProviderSubmitParamDTO = new HeemoneyProviderSubmitParamDTO(providerSubmitCommonDTO, providerSubmitBusinessDTO);
        log.info("http请求汇收银服务层商户统一进件接口入参：{}", heemoneyProviderSubmitParamDTO.getJsonParam());
        try {
            String resultStr = okHttpUtils.postJsonParams(new HeemoneyConfigDTO().getHeemoneyProviderSubmit().getRequest_url(), heemoneyProviderSubmitParamDTO.getJsonParam());
            log.info("http请求汇收银服务层商户统一进件接口回执:{}", resultStr);
            //修改进件日志信息表
            this.updateSubmitInfo(resultStr, submitHeemoneylogDO);
            //返回调用信息
            return ResultUtils.resultStr(resultStr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("http请求汇收银服务层商户统一进件接口失败：{}", e.getMessage());
        }
        return ApiResult.failure();
    }

    private void updateSubmitInfo(String resultStr, FinancialSubmitHeemoneylogDO submitHeemoneylogDO) {
        Map<String, Object> strMap = JsonUtils.jsonToMap(resultStr);
        Object temp = "";
        temp = strMap.get("sub_mch_id");
        String sub_mch_id = temp != null ? temp.toString() : null;
        String result_code = strMap.get("result_code").toString();
        String return_msg = strMap.get("return_msg").toString();
        try {
            submitHeemoneylogDO.setGmtModified(new Date());
            submitHeemoneylogDO.setIsDelete(FinancialRegisterHeemoneylogEnumConstants.ENUM_ISDELETE_UNDEL);
            submitHeemoneylogDO.setMchUid(sub_mch_id.substring(sub_mch_id.indexOf("：") + 1).trim());
            if (!result_code.equals("SUCCESS")) {
                submitHeemoneylogDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_FAIL);
            } else {
                submitHeemoneylogDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_SUC);
            }
            submitHeemoneylogDO.setReturnMessage(return_msg);
            financialSubmitHeemoneylogDao.update(submitHeemoneylogDO);
        } catch (Exception e) {
            log.error("平台商户号：{}修改统一进件日志信息失败！:{}", submitHeemoneylogDO.getStoreNo());
            e.printStackTrace();
        }
    }

    private FinancialSubmitHeemoneylogDO saveRequest(HeemoneyProviderSubmitInParamDTO heemoneyProviderSubmitInParamDTO) {
        FinancialSubmitHeemoneylogDO prosubmitlogDO = new FinancialSubmitHeemoneylogDO();
        try {
            prosubmitlogDO.setGmtCreate(new Date());
            prosubmitlogDO.setIsDelete(FinancialRegisterHeemoneylogEnumConstants.ENUM_ISDELETE_UNDEL);
            prosubmitlogDO.setMchUid(heemoneyProviderSubmitInParamDTO.getMch_uid());
            prosubmitlogDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_WAIT);
            financialSubmitHeemoneylogDao.save(prosubmitlogDO);
        } catch (Exception e2) {
            log.error("请求汇收银商户统一进件参数日志保存失败！商户号为:{}", heemoneyProviderSubmitInParamDTO.getMch_uid());
            e2.printStackTrace();
        }
        return prosubmitlogDO;
    }

    @Override
    public FinancialProsubmitHeemoneylogDO get(Integer id) {
        return financialProsubmitHeemoneylogDao.get(id);
    }

    @Override
    public List<FinancialProsubmitHeemoneylogDO> list(Map<String, Object> map) {
        return financialProsubmitHeemoneylogDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return financialProsubmitHeemoneylogDao.count(map);
    }

    @Override
    public int save(FinancialProsubmitHeemoneylogDO financialProsubmitHeemoneylog) {
        return financialProsubmitHeemoneylogDao.save(financialProsubmitHeemoneylog);
    }

    @Override
    public int update(FinancialProsubmitHeemoneylogDO financialProsubmitHeemoneylog) {
        return financialProsubmitHeemoneylogDao.update(financialProsubmitHeemoneylog);
    }

    @Override
    public int remove(Integer id) {
        return financialProsubmitHeemoneylogDao.remove(id);
    }

    @Override
    public int batchRemove(Integer[] ids) {
        return financialProsubmitHeemoneylogDao.batchRemove(ids);
    }


}
